package com.fanfanwang.ffw.app.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Vibrator;
import android.text.TextUtils;

import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.fanfanwang.ffw.app.receiver.OneShotAlarm;

/** 
 *  
 * 短信推送服务类，在后台长期运行，每个一段时间就向服务器发送一次请求 
 *  
 * @author xfchen
 *  
 */  
public class PushNewMessageService extends Service {  
    private MyThread myThread;  
    private NotificationManager manager;  
    private Notification notification;  
    private PendingIntent pi;  
    private boolean flag = true;  
	
	public static IGetIsNewMessage iGetIsNewMessage;
	private MediaPlayer mMediaPlayer;
	private Vibrator vibrator;//震动的
    @Override  
    public IBinder onBind(Intent intent) {  
        // TODO Auto-generated method stub  
        return null;  
    }  
  
    @Override  
    public void onCreate() {  
    	super.onCreate();
        System.out.println("oncreate()");  
        this.myThread = new MyThread();  
        this.myThread.start();
        initIsNewMessage();
    }  
  
    private void initIsNewMessage() {
		// TODO Auto-generated method stub
    	iGetIsNewMessage = new IGetIsNewMessage() {


			@Override
			public void setIsNewMessage() {
				// TODO Auto-generated method stub
//				notification(AppConstant.URLDecoderString(messageBean.title));
			}
		};
	}

	@Override  
    public void onDestroy() {  
    	super.onDestroy();  
        this.flag = false;  
    }  
  
    private void notification(String content) {  
        // 获取系统的通知管理器  
//        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
//        notification = new Notification(R.drawable.ic_launcher, content,  
//                System.currentTimeMillis());  
//        notification.defaults = Notification.DEFAULT_ALL; // 使用默认设置，比如铃声、震动、闪灯  
//        notification.flags = Notification.FLAG_AUTO_CANCEL; // 但用户点击消息后，消息自动在通知栏自动消失  
//        notification.flags |= Notification.FLAG_NO_CLEAR;// 点击通知栏的删除，消息不会依然不会被删除  
        //让铃声响，开启震动，开启闹铃
    	
    }
    private void startAlarmClock(){
    	  Intent intent = new Intent(this, OneShotAlarm.class);
          PendingIntent sender = PendingIntent.getBroadcast(
                  this, 0, intent, 0);

          // We want the alarm to go off 10 seconds from now.
          Calendar calendar = Calendar.getInstance();
          calendar.setTimeInMillis(System.currentTimeMillis());
          calendar.add(Calendar.SECOND, 10);

          // Schedule the alarm!
          AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
          am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
    }
	/**
	 * 开启震动
	 */
	private void openShock(){
		/* 
		 * 想设置震动大小可以通过改变pattern来设定，如果开启时间太短，震动效果可能感觉不到 
		 * */  
		 vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);  
		long [] pattern = {100,400,100,400};   // 停止 开启 停止 开启   
		vibrator.vibrate(pattern,-1);           //重复两次上面的pattern 如果只想震动一次，index设为-1   
	}

    /**
     * 开启默认铃声
     */
    private void startRing() {  
    	Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
		Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
		r.play();
      /*  mMediaPlayer = MediaPlayer.create(this, getSystemDefultRingtoneUri());  
        mMediaPlayer.setLooping(true);  
        try {  
            mMediaPlayer.prepare();  
        } catch (IllegalStateException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        mMediaPlayer.start(); */ 
    } 
    
  //获取系统默认铃声的Uri  
    private Uri getSystemDefultRingtoneUri() {  
        return RingtoneManager.getActualDefaultRingtoneUri(this,  
                RingtoneManager.TYPE_RINGTONE);  
    }  
	private class MyThread extends Thread {
        @Override  
        public void run() {  
            while (flag) {  
                System.out.println("发送请求");  
                try {
                    // 每10秒向服务器发送一次请求  
                    Thread.sleep(30 * 1000);
                   //TODO 调用异步 ，异步里面如何条件满足，就会回调这里面的接口。然后在里面做震动操作
//                    new GetIsNewMessageAsyncTask(null).execute("");
//                    new GetMessageListAsyncTask(null).execute("");
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }
            }
        }
    }
    
    public interface IGetIsNewMessage{
    	void setIsNewMessage();
    }
}  